﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace TLWindowsEditorWinFormsDemo
{
    /// <summary>
    /// 文档帮助类
    /// by IT_MAOFY 2024/02/01
    /// </summary>
    class DocHelper
    {

        /// <summary>
        /// 读取文件内容
        /// *注意* 文件统一放在指定的文件夹下，并设置为嵌入的资源
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="directory">目录名称</param>
        /// <returns></returns>
        public static string GetContent(string fileName, string directory = "Doc")
        {
            string templateFullName = string.Empty;
            try
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                templateFullName = $"{assembly.GetName().Name}.{directory}.{fileName}";

                using (Stream stream = assembly.GetManifestResourceStream(templateFullName))
                {
                    using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"不能读取文件【{templateFullName}】{ex.Message}");
            }
        }

        /// <summary>
        /// 读取文件流
        /// *注意* 文件统一放在指定的文件夹下，并设置为嵌入的资源
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="directory">目录名称</param>
        /// <returns></returns>
        public static byte[] GetFile(string fileName, string directory = "Doc")
        {
            string templateFullName = string.Empty;
            try
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                templateFullName = $"{assembly.GetName().Name}.{directory}.{fileName}";

                using (var fs = assembly.GetManifestResourceStream(templateFullName))
                {
                    byte[] data = new byte[fs.Length];
                    fs.Read(data, 0, (int)fs.Length);
                    fs.Close();
                    return data;
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"不能读取文件【{templateFullName}】{ex.Message}");
            }
        }
    }
}
